package com.controller;

import com.biz.IAddressBiz;
import com.entity.Address;
import com.entity.Userinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/address")
public class AddressAction {

    @Autowired
    private IAddressBiz biz;

    public IAddressBiz getBiz() {
        return biz;
    }

    public void setBiz(IAddressBiz biz) {
        this.biz = biz;
    }

    //列表
    @RequestMapping("/list")
    public String getAll(HttpSession session)
    {
        List<Address> list=biz.getAll();
        session.setAttribute("alist",list);
        return "/address_list";
    }

    //登录人的地址列表
    @RequestMapping("/getListByUid")
    @ResponseBody
    public List<Address> getListByUid(HttpSession session)
    {
        Userinfo userinfo=(Userinfo)session.getAttribute("loger");
        List<Address> list=biz.getListByUid(userinfo.getId());
        return list;
    }

    //增加
    @RequestMapping("/add")
    public String add(Address aa,HttpSession session)
    {
        Userinfo userinfo=(Userinfo)session.getAttribute("loger");
        aa.setUid(userinfo);
        biz.add_address(aa);
        return "/address_add";
    }


    //删除
    @RequestMapping("/del")
    public String del(int id)
    {
        biz.del_address(id);
        return "/address_list";
    }



    //根据id 获取
    @RequestMapping("/getbyid")
    public ModelAndView getById(int id)
    {
        Address aa=biz.getById(id);
        ModelAndView mv=new ModelAndView();
        mv.addObject("a",aa);
        mv.setViewName("address_mod");
        return mv;
    }


    //修改
    public String mod(Address a)
    {
        biz.mod_address(a);
        return "/address_list";
    }

}
